package org.harsh.service;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.ParameterizedType;

public class Service4DaoImpl<M> implements Service4Dao<M>, InitializingBean {
    protected M mapper;

    @Autowired
    protected SqlSession sqlSession;
    @Override
    public M setMapper() {
        Class clz = (Class)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return (M) sqlSession.getMapper(clz);
    }

    @Override
    public M getMapper() {
        return mapper;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        mapper = setMapper();
    }
}
